package com.panfeng.xcloud.boss.provider.member.utils;

import org.springframework.util.Assert;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String FORMAT_YYYYMMDD = "yyyyMMdd";
    public static String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
    public static String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String FORMAT_IOS = "yyyy/MM/dd HH:mm:ss";
    public static String FORMAT_MINUTE = "yyyy-MM-dd HH:mm";

    public static Date getNow() {
        return new Date(System.currentTimeMillis());
    }

    public static long getNowTimestamp() {
        return getNow().getTime();
    }

    public static final String toMailDateDtPartString(Date aDate) {
        return toFormatDateString(aDate, "yyyyMMdd");
    }

    public static final String toMailDateString(Date aDate) {
        return toFormatDateString(aDate, "yyyyMMddHHmmss");
    }

    public static final String toFullDateCompactString(Date aDate) {
        return toFormatDateString(aDate, "yyyyMMddHHmmssSSS");
    }

    public static final String toFormatDateString(Date aDate, String formatStr) {
        if (aDate == null) {
            return "";
        }
        Assert.hasText(formatStr);
        return new SimpleDateFormat(formatStr).format(aDate);
    }

    /**
     * yyyy-MM-dd HH:mm:ss格式
     *
     * @param date
     * @return
     */
    public static String dateToStr(Date date) {
        return new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS).format(date);
    }

    /**
     * 获取yyyy-MM-dd格式
     *
     * @param date
     * @return
     */
    public static String getStringByDate(Date date) {
        return new SimpleDateFormat(FORMAT_YYYY_MM_DD).format(date);
    }

    /**
     * 在提供的date的基础上添加days，返回格式为yyyy-MM-dd
     *
     * @param date
     * @param days
     * @return
     */
    public static String addDays(Date date, int days) {
        return addDays(date, days, FORMAT_YYYY_MM_DD);
    }

    public static String addDays(Date date, int days, String dateFromat) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, days);
            return parseDateToStr(calendar.getTime(), dateFromat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 格式化Date时间
     *
     * @param time       Date类型时间
     * @param timeFromat String类型格式
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat) {
        return new SimpleDateFormat(timeFromat).format(time);
    }

    /**
     * 获取过期时间
     *
     * @param date
     * @param time
     * @return
     */
    public static String getExpiryTime(Date date, Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
        Date afterDate = new Date(date.getTime() + time);
        return sdf.format(afterDate);
    }

    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static boolean compareDate(Date date1, Date date2) {
        long beginMillisecond = date1.getTime();
        long endMillisecond = date2.getTime();
        if (beginMillisecond > endMillisecond) {
            return true;
        }
        return false;
    }


}
